<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Document</title>
	<script src="./jquery.js"></script>
</head>
<body>
	用户：<input type="text" name="user" id="user"><br>
	密码：<input type="password" name="pwd" id="pwd"><br>
	<input type="submit" value="注册" id="reg">
	<input type="submit" value="登录" id="login">

	<script>
	$(function() {
		function valiData(url, type, dataType, data) {
			$.ajax({
				url: url,
				type: type || 'get',
				data: data || {},
				dataType: dataType || 'text',
				success: function(data) {
					if (data.err) {
						alert(`错误：${data.msg}`);
					} else {
						alert(`成功：${data.msg}`);
					}
				},
				error: function() {
					alert('错误')
				}
			})
		}


		$('#reg').click(function() {
			valiData('/reg','post', 'json', {
				user: $('#user').val(),
				pwd: $('#pwd').val()
			});
		})

		$('#login').click(function() {
			valiData('/login','post','json', {
				user: $('#user').val(),
				pwd: $('#pwd').val()
			});
		})
	})
		
	</script>
</body>
</html>